from django.shortcuts import render, HttpResponse
from django.http import JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from django.views.decorators.csrf import csrf_exempt
import random
# Create your views here.

photo_urls = ['https://s1.328888.xyz/2022/08/10/45pXj.jpg',
              'https://s1.328888.xyz/2022/08/10/45Xjr.jpg',
              'https://s1.328888.xyz/2022/08/10/45YcK.jpg',
              'https://s1.328888.xyz/2022/08/10/45RN7.jpg',
              'https://s1.328888.xyz/2022/08/10/45nSm.jpg',
              'https://s1.328888.xyz/2022/08/10/45Zak.jpg',
              'https://s1.328888.xyz/2022/08/10/45iJE.jpg']
price_list = [11, 34, 25, 54, 85, 344, 63]

tag_list = [
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 2, 'src': '_tag2'}, {'tag_id': 3, 'src': '_tag3'}],
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 2, 'src': '_tag2'}, {'tag_id': 6, 'src': '_tag6'}],
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 4, 'src': '_tag4'}],
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 2, 'src': '_tag2'}, {'tag_id': 3, 'src': '_tag3'}],
    [{'tag_id': 2, 'src': '_tag2'}, {'tag_id': 3, 'src': '_tag3'}],
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 2, 'src': '_tag2'}],
    [{'tag_id': 1, 'src': '_tag1'}, {'tag_id': 2, 'src': '_tag2'}, {'tag_id': 3, 'src': '_tag3'}],

]




class test(APIView):
    @csrf_exempt
    def post(self, request, *args, **kwargs):
        data = request.data
        print(data, type(data))
        print(data['num'])
        print(data['name'])
        # return Response({'status': True})
        if not data['name'] == 'admin':
            return Response({"status": True})
        # print('this step 1')
        res = {"Content-Type": "application/json"}
        res['status'] = 200
        shop_list = []
        for ind in range(int(data['num'])):
            temp = {}
            temp['id'] = ind + 1
            temp['name'] = '商品' + str(ind + 1)
            temp['intro'] = '这个人太懒了什么也没写'
            temp['image'] = photo_urls[ind]
            temp['price'] = price_list[ind]
            shop_list.append(temp)
        res['data'] = shop_list
        return Response(res)


# @csrf_exempt
def bc(request):
    # print(request.method)
    res = {"Content-Type": "application/json"}
    name = request.POST.get('name')
    res['name'] = name
    num = request.POST.get('num')
    shop_list = []
    for ind in range(int(num)):
        temp = {}
        temp['id'] = ind + 1
        temp['title'] = '商品' + str(ind + 1)
        temp['intro'] = '什么也没写'
        temp['image'] = photo_urls[ind]
        temp['price'] = price_list[ind]
        temp['tag'] = tag_list[ind]
        shop_list.append(temp)
    res['goods'] = shop_list
    return JsonResponse(res)

brand_list = ['Lenovo/联想', 'Dell/戴尔', 'ASUS/华硕', 'Apple/苹果', 'acer/宏碁', '神船']
CPU = ['i5', 'i7','i3' ,'e3' ,'e5' ,'m1','r7']
RAM = [str(mem) + 'G' for mem in range(2 ,17, 2)]
DISK= [str(ssd) + 'T' for ssd in range(1,5)]

def bc1(request):
    temp = {"Content-Type": "application/json"}
    name = request.POST.get('name')
    temp['request_name'] = name
    temp['title'] = random.choice(brand_list)+'的电脑'
    temp['intro'] = '什么也没写'
    temp['image'] = random.choice(photo_urls)
    temp['price'] = random.choice(price_list)
    temp['tag'] = random.choice(tag_list)
    temp['cpu'] = random.choice(CPU)
    temp['ram'] = random.choice(RAM)
    temp['disk'] = random.choice(DISK)
    return JsonResponse(temp)

def demo(request):
    return HttpResponse('访问成功')
